package com.yima.baseproj.module.app.equipmentUser;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yima.baseproj.config.exception.type.ApiException;
import com.yima.baseproj.module.config.BaseController;
import com.yima.baseproj.module.config.utils.PageUtil;
import com.yima.baseproj.module.config.utils.PageVo;
import com.yima.baseproj.module.app.equipmentUser.model.*;
import com.yima.baseproj.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;


/**
 * equipment_user
 *
 * @author 一码
 * @createDate 2024-01-06 15:46:35
 * @contact 1424529270@qq.com
 **/
@Slf4j
@Api(tags = "用户数据")
@RestController
@RequestMapping("api/equipment/user")
public class EquipmentUserController extends BaseController {

    @Resource
    private EquipmentUserService equipmentUserService;

    @ApiOperation("查询分页")
    @GetMapping(value = "/findAllByPage")
    public R<Object> findAllByPage(PageVo page, EquipmentUserSearchModel searchVo) {
        Page<Map> pageData = equipmentUserService.findByPage(PageUtil.initMpPage(page), searchVo);
        return R.success(pageData,"操作成功");
    }

    @ApiOperation("查询列表")
    @GetMapping(value = "/findAllByList")
    public R<Object> findAllByList(EquipmentUserSearchModel searchVo) {
        List<Map> list = equipmentUserService.findByList(searchVo);
        return R.success(list,"操作成功");
    }

    @ApiOperation("查询详情")
    @GetMapping(value = "/findById/{id}")
    public R<Map> findById(@PathVariable String id) {
        EquipmentUserEntity equipmentUserEntity = equipmentUserService.getById(id);
        if (equipmentUserEntity == null) {
            throw ApiException.getInstance("数据不存在，请刷新重试");
        }

        Map result = JSON.parseObject(JSON.toJSONString(equipmentUserEntity), Map.class);

        return R.success(result,"操作成功");
    }

    @ApiOperation("添加")
    @PostMapping(value = "/add")
    public void add(@Validated @RequestBody EquipmentUserAddParamModel paramModel) {
        //方式一：适合单一添加
        EquipmentUserEntity equipmentUserEntity = new EquipmentUserEntity();
        equipmentUserEntity.setData(paramModel);
        equipmentUserService.save(equipmentUserEntity);

        //方法二：适合事务添加
//        equipmentUserService.add(paramModel);
    }

    @ApiOperation("修改")
    @PostMapping(value = "/edit")
    public void edit(@Validated @RequestBody EquipmentUserEditParamModel paramModel) {
        //校验参数
        EquipmentUserEntity equipmentUserEntity = equipmentUserService.getById(paramModel.getId());
        if (equipmentUserEntity == null) {
            throw new ApiException("数据不存在，请刷新重试");
        }

        //方式一：适合单一编辑
        equipmentUserEntity.setData(paramModel);
        equipmentUserService.updateById(equipmentUserEntity);

        //方法二：适合事务编辑
//        equipmentUserService.edit(paramModel);
    }

    @ApiOperation("删除")
    @DeleteMapping(value = "/delByIds/{ids}")
    public void delAllByIds(@PathVariable String[] ids) {
        //适合事务删除
        equipmentUserService.dels(ids);
    }
}